package com.zjitc.controller;

import com.zjitc.model.Country;
import com.zjitc.service.CountryService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.RequestWrapper;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/20 16:17
 * @description:
 */
@Controller
public class CountryController {

  @Autowired
  private CountryService countryService;

  @RequestMapping("/findByCode")
  public ModelAndView findByCode(String code){
    Country country=countryService.findByCode(code);
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("country",country);
    modelAndView.setViewName("/view/update.jsp");
    return modelAndView;
  }


  @RequestMapping("/findAllCountry")
  public ModelAndView findAll(){
    List<Country> list=countryService.findAll();
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("country",list);
    modelAndView.setViewName("/view/country.jsp");
    return modelAndView;
  }
  @RequestMapping("/findByCountryName")
  public ModelAndView findByCountryName(HttpServletRequest request){
    String region ='%'+request.getParameter("region")+'%';
    List<Country> list=countryService.findByCountryName(region);
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("country",list);
    modelAndView.setViewName("/view/country.jsp");
    return modelAndView;
  }

  @RequestMapping("/insert")
  public ModelAndView insert(Country country){
    int insert = countryService.insert(country);
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("insert",insert);
    modelAndView.setView(new InternalResourceView("/findAllCountry.do"));
    return modelAndView;
  }

  @RequestMapping("/delete")
  public ModelAndView delete(HttpServletRequest request){
    String code =request.getParameter("code");
    int delete = countryService.delete(code);
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("delete",delete);
    modelAndView.setView(new InternalResourceView("/findAllCountry.do"));
    return modelAndView;
  }

  @RequestMapping("/update")
  public ModelAndView update(Country country){
    countryService.update(country);
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.setView(new InternalResourceView("/findAllCountry.do"));
    return modelAndView;
  }
}
